/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_DOWN_SAMPLING_FIT_CENTER_MEMORY_H
#define IMAGE_KNIFE_C_KEKE_DOWN_SAMPLING_FIT_CENTER_MEMORY_H

#include "down_sampling_base.h"

namespace ImageKnifePro {
class DownSamplingFitCenterMemory : public DownSamplingBase {
public:
    float GetScaleFactor(const Image_Size &sourceSize, const Image_Size &requestSize) override
    {
        IMAGE_KNIFE_CHECK_RETURN(sourceSize.width == 0, 1);
        IMAGE_KNIFE_CHECK_RETURN(sourceSize.height == 0, 1);
        const float percentageFactor = std::min((float)requestSize.width / sourceSize.width,
                                                (float)requestSize.height /sourceSize.height);
        uint32_t width = std::round(percentageFactor * sourceSize.width);
        uint32_t height = std::round(percentageFactor * sourceSize.height);

        //将整型的缩放因子转换为2的次幂采样大小
        IMAGE_KNIFE_CHECK_RETURN(width == 0, 1);
        IMAGE_KNIFE_CHECK_RETURN(height == 0, 1);
        uint32_t scaleFactor = GetFactorAsBit(std::min(sourceSize.width / width, sourceSize.height / height));
        if (percentageFactor != 0 && scaleFactor < (1 / percentageFactor)) {
            scaleFactor <<= 1;
        }
        return scaleFactor;
    }
};
}

#endif //IMAGE_KNIFE_C_KEKE_DOWN_SAMPLING_FIT_CENTER_MEMORY_H
